<?php
include("common.inc.php");
InitGP(array("action","gid","ref","group_type")); //初始化變量全局返回
include(INC_PATH."/group.class.php"); //包含團購類
$gp=new GroupClass();
//判斷後台是否設置首頁顯示團購ID
if(GetNum($gid)){
	$gid=GetNum($gid);
}else {
	//自動顯示當天ID團購
	$todaystart=strtotime(date('Y-m-d 0:0:0',time()));
	$todayend=strtotime(date('Y-m-d 0:0:0',time()+3600*24));
	$wheresql="group_begin_time >{$todaystart} and group_begin_time < {$todayend}";
	//$wheresql="TO_DAYS(group_begin_time) = TO_DAYS(NOW())";
	$returnarray = $gp->getdata(1,"",$wheresql);
	//print_r($returnarray);
	$gid=$returnarray[0]['gid'];//設置查詢到的gid
	unset($returnarray);	//清空數組
}

if(empty($gid))dheader("mail.php");//php跳轉頁面

$value=$gp->getone($gid); 	//獲取團購信息

$group_type = $value['group_type'] ;


include(INC_PATH."/guestbook.class.php"); //包含團購類
$gb=new GuestBook();
$gbcount=$gb->getcount("state=1");	//獲取留言總數
$gbarray=$gb->getdata(3,"state=1");	//獲取三條留言

include(INC_PATH."/article.class.php");
$art=new ArticleClass();
$datagg=$art->getdata('','typeid=3');
$subject = "{$value['title']}";
$body = "那武價吼康啦【{$cfg_site_name}】今天端出來的超值好康就是...
登登登~
『{$value['title']}』
手腳慢的人就搶不到啦!!!
{$cfg_site_url}";
$subject= iconv("UTF-8","BIG5//IGNORE", $subject);
$body= iconv("UTF-8","BIG5//IGNORE", $body);
$subject = urlencode($subject);
$body = urlencode($body);

/* 取得尚未過期的團購資訊 */
unset($cwherestr);
unset($gwherestr);
//$todaystart=strtotime(date('Y-m-d 0:0:0',time()));
	$todaystart=time();
	$cwherestr[]="state=1";
	$cwherestr[]="group_end_time >={$todaystart}";
	$cwherestr[]="group_begin_time < {$todaystart}";
	$gwherestr[]="G.state=1";
	$gwherestr[]="G.group_end_time >={$todaystart}";
	$gwherestr[]="G.group_begin_time < {$todaystart}";	
	if(!empty($cwherestr)) $cwheresql = implode(' AND ', $cwherestr);	//條件匯總
	if(!empty($gwherestr)) $gwheresql = implode(' AND ', $gwherestr);	//條件匯總
	
	
	//獲取當前頁碼
	$total=$gp->getcount($cwheresql); 							  //總信息數
	$pagesize=10;												  //一頁顯示信息數
	if(!empty($cfg_site_pagenum))$pagesize=$cfg_site_pagenum;
	$page = isset($page) ? max(1, intval($page)) : 1;             //處理頁碼變量
	$offset=($page-1)*$pagesize;   								  //偏移量
	$dataarray=$gp->getdata("$offset,$pagesize","IMG",$gwheresql,"","G.*,A.filepath,A.thumb"); //獲取團購數據

/*****合作網站 導入buy917 Save Session*****/
if(is_null($_SESSION['referid']) && is_null($_SESSION['refer']) ){
//if (isset($_SERVER['HTTP_REFERER']) && $refer[2]=="www.123.com.tw"){	
	if(isset($ref) && $ref==md5("www.123.com.tw"."-".$gid)){
		$_SESSION['refer'] = "www.123.com.tw";
		$_SESSION['referid'] = GetNum($gid);
		$_SESSION['hostname'] = urlencode($ref);
	}else if(isset($ref) && $ref==md5("www.mycoupon.com.tw"."-".$gid)){
		$_SESSION['refer'] = "www.mycoupon.com.tw";
		$_SESSION['referid'] = GetNum($gid);
		$_SESSION['hostname'] = urlencode($ref);
	}else{
		$_SESSION['refer'] = "buy917.com";
		$_SESSION['hostname'] = "buy917.com";
		//$_SESSION['referid'] = "";//GetNum($gid);
	}
}
/*****合作網站 導入buy917 Save Session*****/
/*****合作網站 show Logo*****/
if($_SESSION['refer'] == "www.mycoupon.com.tw"){
	echo '<div style="position: absolute;  top:50px; left:50px; ">
			<a href="http://www.mycoupon.com.tw" target="_blank">
				<img src='.SITE_URL.'/images/mycpuopn/logo_mycoupon_4.gif width="115px" />
			</a>
		</div>';
}
/*****合作網站 show Logo*****/




/*
$refer = split("/",$_SERVER['HTTP_REFERER']);
if(is_null($_SESSION['referid']) && is_null($_SESSION['refer'])){
//if (isset($_SERVER['HTTP_REFERER']) && $refer[2]=="www.123.com.tw"){	
if(isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER']=="http://squallfan.dyndns.org/buy917/testcase.php"){	
	$_SESSION['refer'] = $_SERVER['HTTP_REFERER'];
	$_SESSION['referid'] = GetNum($gid);
	$_SESSION['hostname'] = urlencode($refer[2]);
}else{
	$_SESSION['refer'] = "buy917.com";
	$_SESSION['hostname'] = "buy917.com";
	//$_SESSION['referid'] = "";//GetNum($gid);
}
}*/
//echo md5("www.123.com.tw"."-".$gid);
//echo "[".$_SESSION['refer']."]";
include template('view');//包含輸出指定模板
?>